package com.cloud.control.service.socket.entity;

import java.io.Serializable;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import com.cloud.control.service.core.ProxyManageServices;
import com.cloud.control.service.utils.ProxyConstant;

public class ProxyEntity  implements Serializable {

	private static final long serialVersionUID = -3278072549210826376L;
    private final static Log log = LogFactory.getLog(ProxyEntity.class);
	

	private String header="proxy";
	private String commandIn;
	private String commandOut;
	private String rsaCert;
	private String trustIPs;//返回的可信任ip地址序列!逗号分隔
	private String status="new";  //状态: new,verified(表示通过了验证！)
	private String ipAddress;
	private String port;
	private int    sinaEmailSendThreadCount=3;//允许sina email发送线程的数量！
	private long   allocateTime=0;
	private String userid;
	private String name;//台中--冰晶--准銅--聯係電話   格式
	//代理的connection
	public IoSession session;
	
	public IoSession getSession() {
		return session;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}
	/*通过反射的方法,设置任务类型和对应的connection数量限制*/
	private HashMap<String,Integer> task=new HashMap<String,Integer>();
	
	/*初始化代理实体,hard coding 允许的代理type和数量*/
	public ProxyEntity(){
		initProxyTask();
	}

	/**
	 * 初始化proxy的任务列表！
	 */
	public void initProxyTask() {
		setTaskLimit("sinaEmailSend",5);
		setTaskLimit("sinaWeiboSend",3);
		setTaskLimit("sinaEmailApply",1);
		/****************************/
		setTaskLimit("QQEmailSend",5);
		setTaskLimit("QQApply",1);
		setTaskLimit("QQWeibo",3);
		/************自定义的task，支持多个线程****************/
		setTaskLimit("Task00",1);
		setTaskLimit("Task01",1);
		setTaskLimit("Task02",1);
		setTaskLimit("Task03",1);
		setTaskLimit("Task04",1);
		setTaskLimit("Task05",1);//临时sina发送
		setTaskLimit("Task06",1);
		setTaskLimit("Task07",1);
		setTaskLimit("Task08",1);
		setTaskLimit("Task09",1);
		setTaskLimit("Task10",3);//sina email send
		setTaskLimit("Task11",3);
		setTaskLimit("Task12",3);
		setTaskLimit("Task13",3);
		setTaskLimit("Task14",3);
		setTaskLimit("Task15",3);
		setTaskLimit("Task20",5);
		setTaskLimit("Task21",5);
		setTaskLimit("Task22",5);
		setTaskLimit("Task23",5);
		setTaskLimit("Task24",5);
		setTaskLimit("Task25",5);
		setTaskLimit("Task30",10);//Online QQ scan
		setTaskLimit("Task31",10);
		setTaskLimit("Task32",10);
		setTaskLimit("Task33",10);
		setTaskLimit("Task34",10);
		setTaskLimit("Task35",10);
		setTaskLimit("Task40",20);
		setTaskLimit("Task41",20);
		setTaskLimit("Task42",20);
		setTaskLimit("Task43",20);
		setTaskLimit("Task44",20);
		setTaskLimit("Task45",20);
		setTaskLimit("Task50",50);
		setTaskLimit("Task51",50);
		setTaskLimit("Task52",50);
		setTaskLimit("Task53",50);
		setTaskLimit("Task54",50);
		setTaskLimit("Task55",50);

		setTaskLimit("TaskLongLive",100);
	}
	
	public  void setTaskLimit(String taskType,int count){
		task.put(taskType, new Integer(count));
	}
	
	/**根据proxy的能力，将其全部可用的connection分配出去！返回分配的数量**/
	public synchronized int allocTask(String taskType,int taskCount){
		long now=System.currentTimeMillis();
		int count=0;
		
		/**代理的全部connection都分配了，代理服務器只要在Queue中，就允許繼續使用！**/
		if(task.isEmpty()==true){ //|| (allocateTime>0  && (now-allocateTime)>=(ProxyConstant.proxySurvivalPeriodinServer-120*1000))
			log.error(userid+" can't be allocated for task:"+taskType);
			//log.error(" task.isEmpty()+:"+task.isEmpty());
			//log.error(" (now-allocateTime):"+(now-allocateTime));
			setStatus("removed");
			session.write("proxy----done#");//向代理发送切换ip命令
			session.close(true);//关闭和代理的socket连接！
			return -1;
		}
		//请求的类型的代理分配完了
		if(task.get(taskType)==null || task.get(taskType).intValue()<=0){
			return 0;
		}
		//可用的connection数量！
		int availTask=task.get(taskType).intValue();
		if(taskCount>=availTask){
			count=availTask;
			task.remove(taskType);//整个代理type都不可用了！
			//task.put(taskType, new Integer(0));
		}else{
			count=taskCount;
			task.put(taskType, new Integer(availTask-taskCount));
		}
		log.info("代理分配："+taskType+"   "+count);
		//allocateTime=System.currentTimeMillis();
		return count;
	}
	
	public String getHeader() {
		return header;
	}
	public String getCommandIn() {
		return commandIn;
	}
	public String getCommandOut() {
		return commandOut;
	}
	public String getRsaCert() {
		return rsaCert;
	}
	public String getTrustIPs() {
		return trustIPs;
	}
	public void setHeader(String header) {
		this.header = header;
	}
	public void setCommandIn(String commandIn) {
		this.commandIn = commandIn;
	}
	public void setCommandOut(String commandOut) {
		this.commandOut = commandOut;
	}
	public void setRsaCert(String rsaCert) {
		this.rsaCert = rsaCert;
	}
	public void setTrustIPs(String trustIPs) {
		this.trustIPs = trustIPs;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getStatus() {
		return status;
	}
	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
	public String getIpAddress() {
		return ipAddress;
	}
	public void setSinaEmailSendThreadCount(int sinaEmailSendThreadCount) {
		this.sinaEmailSendThreadCount = sinaEmailSendThreadCount;
	}
	public int getSinaEmailSendThreadCount() {
		return sinaEmailSendThreadCount;
	}
	public void setAllocateTime(long allocateTime) {
		this.allocateTime = allocateTime;
	}
	public long getAllocateTime() {
		return allocateTime;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUserid() {
		return userid;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getPort() {
		return port;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
}
